This challenge will get you familiar with the basic elements of Python by programming a simple card game. We will create a custom class to represent each player in the game, which will store information about their current pot, as well as a series of methods defining how they play the game. We will also build several functions to control the flow of the game and get data back at the end.

We will start by importing the 'random' library, which will allow us to use its functions for picking a random entry from a list.


In [ ]:
import random

First we will establish some general variables for our game, including the 'stake' of the game (how much money each play is worth), as well as a list representing the cards used in the game. To make things easier, we will just use a list of numbers 0-9 for the cards.


In [ ]:
gameStake = 50  
cards = range(10)

Next, let's define a new class to represent each player in the game. I have provided a rough framework of the class definition along with comments along the way to help you complete it. Places where you should write code are denoted by comments inside [] brackets and CAPITAL TEXT.


In [ ]:
class Player:
    
    # in the __init__() function, use the two input variables to initialize the ID and starting pot of each player
    
    def __init__(self, inputID, startingPot):
        # [CREATE YOUR INITIALIZATIONS HERE]
        # make sure you initialize two local variables to store a unique ID for each player 
        # and the player's current 'pot' of money
        
    # create a function for playing the game. This function starts by taking an input for the dealer's card
    # and picking a random number from the 'cards' list for the player's card

    def play(self, dealerCard):
        # we use the random.choice() function to select a random item from a list
        playerCard = random.choice(cards)
        
        # here we should have a conditional that tests the player's card value against the dealer card
        # and returns a statement saying whether the player won or lost the hand
        # before returning the statement, make sure to either add or subtract the stake from the player's pot so that
        # the 'pot' variable tracks the player's money
        
        if playerCard < dealerCard:
            # [INCREMENT THE PLAYER'S POT, AND RETURN A MESSAGE]
        else:
            # [INCREMENT THE PLAYER'S POT, AND RETURN A MESSAGE]
        
    # create an accessor function to return the current value of the player's pot
    def returnPot(self):
        # [FILL IN THE RETURN STATEMENT]
        
    # create an accessor function to return the player's ID
    def returnID(self):
        # [FILL IN THE RETURN STATEMENT]

Next we will create some functions outside the class definition which will control the flow of the game. The first function will play one round. It will take as an input the collection of players, and iterate through each one, calling each player's '.play() function.


In [ ]:
def playHand(players):
    
    for player in players:
        dealerCard = random.choice(cards)
        #[EXECUTE THE PLAY() FUNCTION FOR EACH PLAYER USING THE DEALER CARD, AND PRINT OUT THE RESULTS]

Next we will define a function that will check the balances of each player, and print out a message with the player's ID and their balance.


In [ ]:
def checkBalances(players):
    
    for player in players:
        #[PRINT OUT EACH PLAYER'S BALANCE BY USING EACH PLAYER'S ACCESSOR FUNCTIONS]

Now we are ready to start the game. First we create an empy list to store the collection of players in the game.


In [ ]:
players = []

Then we create a loop that will run a certain number of times, each time creating a player with a unique ID and a starting balance. Each player should be appended to the empty list, which will store all the players. In this case we pass the 'i' iterator of the loop as the player ID, and set a constant value of 500 for the starting balance.


In [ ]:
for i in range(5):
    players.append(Player(i, 500))

Once the players are created, we will create a loop to run the game a certain amount of times. Each step of the loop should start with a print statement announcing the start of the game, and then call the playHand() function, passing as an input the list of players.


In [ ]:
for i in range(10):
    print('')
    print('start game ' + str(i))
    playHand(players)

Finally, we will analyze the results of the game by running the 'checkBalances()' function and passing it our list of players.


In [ ]:
print('')
print('game results:')
checkBalances(players)

Below is a version of the expected printout if you've done everything correctly (note that since the cards are chosen randomly the actual results will differ, but the structure should be the same).

start game 0
player 0 Lose, 4 vs 7
player 1 Win, 2 vs 0
player 2 Lose, 0 vs 4
player 3 Win, 7 vs 2
player 4 Win, 5 vs 0

start game 1
player 0 Win, 1 vs 0
player 1 Lose, 1 vs 5
player 2 Lose, 6 vs 9
player 3 Lose, 1 vs 8
player 4 Lose, 0 vs 9

start game 2
player 0 Win, 3 vs 3
player 1 Lose, 0 vs 2
player 2 Win, 9 vs 6
player 3 Win, 8 vs 7
player 4 Win, 8 vs 6

start game 3
player 0 Win, 9 vs 7
player 1 Lose, 7 vs 8
player 2 Lose, 2 vs 3
player 3 Lose, 0 vs 8
player 4 Lose, 0 vs 6

start game 4
player 0 Win, 7 vs 4
player 1 Win, 3 vs 0
player 2 Win, 8 vs 5
player 3 Win, 2 vs 1
player 4 Lose, 4 vs 7

start game 5
player 0 Lose, 2 vs 8
player 1 Lose, 4 vs 6
player 2 Win, 2 vs 0
player 3 Lose, 4 vs 5
player 4 Lose, 3 vs 8

start game 6
player 0 Lose, 3 vs 6
player 1 Win, 8 vs 0
player 2 Win, 5 vs 5
player 3 Lose, 2 vs 6
player 4 Win, 8 vs 7

start game 7
player 0 Lose, 0 vs 9
player 1 Lose, 6 vs 8
player 2 Lose, 1 vs 9
player 3 Lose, 4 vs 8
player 4 Win, 9 vs 8

start game 8
player 0 Lose, 1 vs 8
player 1 Lose, 3 vs 9
player 2 Win, 5 vs 4
player 3 Win, 6 vs 2
player 4 Win, 3 vs 0

start game 9
player 0 Lose, 5 vs 6
player 1 Win, 6 vs 1
player 2 Lose, 8 vs 9
player 3 Lose, 3 vs 9
player 4 Win, 7 vs 5

game results:
player 0 has $400 left.
player 1 has $400 left.
player 2 has $500 left.
player 3 has $400 left.
player 4 has $600 left.